package org.prothliberger.net.servlet;

import java.io.IOException;
import java.util.Properties;

public class ConfigurableServletEngine extends AbstractServletEngine {
	private Properties config = new Properties();

	private Servlet inconnu = new FonctionInconnu();

	public ConfigurableServletEngine(int aPort) {
		super(aPort);
		try {
			config.load(getClass().getResourceAsStream(
					"ServletMapping.properties"));
		} catch (IOException e) {
			throw new IllegalStateException(
					"erreur de chargement de la config", e);
		}
	}

	@Override
	protected Servlet resolveServletString(String aNomFonction) {
		String vServletClassName = config.getProperty(aNomFonction);
		if (vServletClassName == null) {
			return inconnu;
		}
		Servlet vServlet;
		try {
			vServlet = (Servlet) Class.forName(vServletClassName).newInstance();
		} catch (Exception e) {
			throw new IllegalArgumentException(
					"erreur d'instanciation du servlet,  class="
							+ vServletClassName, e);
		}
		return vServlet;
	}

}
